package com.legal.utils;

import com.huawei.sis.bean.AuthInfo;
import com.huawei.sis.bean.SisConfig;
import com.huawei.sis.bean.SisConstant;
import com.huawei.sis.bean.request.AsrCustomShortRequest;
import com.huawei.sis.bean.response.AsrCustomShortResponse;
import com.huawei.sis.client.AsrCustomizationClient;
import com.huawei.sis.exception.SisException;
import com.huawei.sis.util.IOUtils;
import com.huawei.sis.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 华为云语音识别(SIS)工具类
 * @author xiaopeng
 * @version 1.0
 */
public class HuaweiSisUtil {
    // 单例实例
    private static volatile HuaweiSisUtil instance;

    // 客户端实例
    private AsrCustomizationClient asrClient;

    // 默认配置
    private static final String DEFAULT_REGION = "cn-north-4";
    private static final String DEFAULT_AUDIO_FORMAT = "wav";
    private static final String DEFAULT_PROPERTY = "chinese_8k_common";

    // 私有构造方法
    private HuaweiSisUtil(String ak, String sk, String region, String projectId) {
        AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId);
        SisConfig config = getDefaultConfig();
        this.asrClient = new AsrCustomizationClient(authInfo, config);
    }

    /**
     * 获取单例实例
     */
    public static HuaweiSisUtil getInstance(String ak, String sk, String region, String projectId) {
        if (instance == null) {
            synchronized (HuaweiSisUtil.class) {
                if (instance == null) {
                    instance = new HuaweiSisUtil(ak, sk, region, projectId);
                }
            }
        }
        return instance;
    }

    /**
     * 获取默认配置
     */
    private SisConfig getDefaultConfig() {
        SisConfig config = new SisConfig();
        config.setConnectionTimeout(SisConstant.DEFAULT_CONNECTION_TIMEOUT);
        config.setReadTimeout(SisConstant.DEFAULT_READ_TIMEOUT);
        return config;
    }

    /**
     * 语音识别(短语音)
     * @param audioPath 音频文件路径
     * @param options 可选参数:
     *                - audioFormat: 音频格式，默认wav
     *                - property: 属性字符串，默认chinese_8k_common
     *                - addPunc: 是否添加标点，默认yes
     *                - digitNorm: 是否将数字转写为阿拉伯数字，默认no
     *                - needWordInfo: 是否需要word_info，默认no
     *                - vocabularyId: 热词ID，可选
     * @return 识别结果
     * @throws SisException 识别异常
     */
    public AsrCustomShortResponse recognizeShortAudio(String audioPath, Map<String, String> options) throws SisException {
        // 参数处理
        String audioFormat = StringUtils.defaultIfBlank(options.get("audioFormat"), DEFAULT_AUDIO_FORMAT);
        String property = StringUtils.defaultIfBlank(options.get("property"), DEFAULT_PROPERTY);

        // 准备请求
        String data = IOUtils.getEncodeDataByPath(audioPath);
        AsrCustomShortRequest request = new AsrCustomShortRequest(data, audioFormat, property);

        // 设置可选参数
        request.setAddPunc(StringUtils.defaultIfBlank(options.get("addPunc"), "yes"));
        request.setDigitNorm(StringUtils.defaultIfBlank(options.get("digitNorm"), "no"));
        request.setNeedWordInfo(StringUtils.defaultIfBlank(options.get("needWordInfo"), "no"));
        if (StringUtils.isNotBlank(options.get("vocabularyId"))) {
            request.setVocabularyId(options.get("vocabularyId"));
        }

        // 发送请求
        AsrCustomShortResponse response = asrClient.getAsrShortResponse(request);
//        return JsonUtils.obj2Str(response, true);
        return response;
    }

    /**
     * 简化版语音识别(使用默认参数)
     */
    public AsrCustomShortResponse recognizeShortAudio(String audioPath) throws SisException {
        return recognizeShortAudio(audioPath, new HashMap<>());
    }
}